package com.jackchong.utils;

import androidx.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by Red on 2017/4/24.
 */

public abstract class CommonAdapder<T> extends BaseAdapter {

    protected List<T> list;
    protected int layout;
    protected int count;
    protected boolean isAuto;

    private OnItemLongClickListener<T> mOnItemLongClickListener;
    private OnItemClickListener<T> mOnItemClickListener;

    public CommonAdapder(List<T> list, int layout) {
        this(list, layout, false);
    }

    public CommonAdapder(List<T> list, int layout, boolean isAuto) {
        this(list, layout, isAuto, 0);
    }

    public CommonAdapder(List<T> list, int layout, boolean isAuto, int count) {
        this.list = list;
        this.layout = layout;
        this.isAuto = isAuto;
        this.count = count;
    }

    public CommonAdapder(List<T> list, int layout, int count) {
        this(list, layout, false, count);
    }

    public void setData(List<T> list){
        this.list = list;
        super.notifyDataSetChanged();
    }

    public void addData(T bean){
        if (list != null){
            list.add(bean);
        }
        super.notifyDataSetChanged();
    }

    public int getMoreCount(){
        return count;
    }

    @Override
    public int getCount() {
        return list == null ? count < 0 ? 0 : count : list.size() + count < 0 ? 0 : list.size() + count;
    }

    @Override
    public T getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommonViewHolder holder =  CommonViewHolder.creatCViewHolder(convertView, layout, parent, isAuto);

        holder.convertView.setOnClickListener(view -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onClick(holder.convertView, list == null || list.size() <= position ? null : list.get(position), position);
            }
        });
        holder.convertView.setOnLongClickListener(view -> {
            if (mOnItemLongClickListener != null) {
                return mOnItemLongClickListener.onClick(holder.convertView, list == null || list.size() <= position ? null : list.get(position), position);
            }
            return false;
        });

        setData(holder, list == null || list.size() <= position ? null : list.get(position), position);
        return holder.convertView;
    }


    protected abstract void setData(CommonViewHolder holder, T bean, int position);

    public List<T> getData() {
        return list;
    }

    public T getData(int position) {
        return list == null ? null : list.get(position);
    }

    public interface OnItemClickListener<T>{
        void onClick(View contentView, T bean, int position);
    }
    public interface OnItemLongClickListener<T>{
        boolean onClick(View contentView, T bean, int position);
    }

    public OnItemClickListener<T> getOnItemClickListener(){
        return mOnItemClickListener;
    }

    public OnItemLongClickListener getOnItemLongClickListener(){
        return mOnItemLongClickListener;
    }

    public void setOnItemClickListener(@Nullable OnItemClickListener<T> onItemClickListener){
        mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(@Nullable OnItemLongClickListener<T> onItemLongClickListener){
        mOnItemLongClickListener = onItemLongClickListener;
    }
}
